#region License
/*

	Copyright (C) 2008 Oliver Charles, Daniel Hollander

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
	
*/
#endregion

using System;
using Moebius.Hud;
using Moebius.Core;
using Tao.Sdl;
using Tao.OpenGl;
using System.Drawing;

namespace Moebius.Rendering.OpenGL
{
	public class Renderer : Rendering.RenderingDevice
	{	
		public override int ExtraSdlOptions
		{
			get { return Sdl.SDL_OPENGL; }
		}
		
		public override void Initialize(IntPtr windowHandle, Size screenSize,
			GraphicsOptions options)
		{
			base.Initialize(windowHandle, screenSize, options);
			
			// Setup SDL OpenGL
			Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_DOUBLEBUFFER, 1);
			
			// Initialize OpenGL
			Gl.glClearColor(0, 0, 0, 0);
			Gl.glViewport(0,0,800,600);
			
			Gl.glMatrixMode(Gl.GL_PROJECTION);
			Gl.glLoadIdentity();
			Gl.glOrtho(0, 800, 600, 0, -100, 100);
			
			Gl.glBlendFunc(Gl.GL_SRC_ALPHA, Gl.GL_ONE_MINUS_SRC_ALPHA);
			Gl.glEnable(Gl.GL_BLEND);

			Gl.glEnable(Gl.GL_TEXTURE_2D);
			Gl.glDisable(Gl.GL_LIGHTING);
			
			
			Gl.glMatrixMode(Gl.GL_MODELVIEW);
			
			Subsystem = new RenderingSubsystem(this);
		}
		
		protected override void Render()
		{
			Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
			
			foreach (Image component in Subsystem.Images)
				component.Draw();
				
			if(Hud != null)
				Hud.Render();
			
			Sdl.SDL_GL_SwapBuffers();
		}
		
		protected override HeadsUpDisplay CreateHud()
		{
			return null;
		}
		
		public override void RegisterResourceManager(ResourceFactory factory) 
		{
			factory.RegisterResourceManager(new string[] { ".png" }, new ResourceManager(this));
		}
	}
}